import { Component,OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {HttpURLService} from "../../httpURL.service";

/**
 * 开关阀门因子
 * guopeng
 * 2017.11.27
 */
export class SwitchFactor {
  constructor(
    private id: string,//污染物id
    private switchFactorCode: string,//阀门开关因子编码
    private switchFactorName: string,//阀门开关因子名称
    private switchFactorUnit: string,//阀门开关因子单位
    private switchFactorType: string,//阀门开关因子类型
    private extensionInfo: string//拓展信息
  ){}
}

@Component({
  selector: 'app-switchFactor',
  templateUrl: './switchFactor.component.html',
  styleUrls: ['./switchFactor.component.css']
})
export class SwitchFactorComponent implements OnInit{
  private switchFactorCode : string;//阀门开关因子编码
  private switchFactorName : string;//阀门开关因子名称
  errorMessage:string;//錯誤消息
  private list:Array<any>;
  public queryMessage:SwitchFactor = new SwitchFactor("","","","","","");
  public updateMessages:SwitchFactor = new SwitchFactor("","","","","","");
  public addMessages:SwitchFactor =  new SwitchFactor("","","","","","");

  public params; // 保存页面url参数
  public pageSize:any = 5;// 每页数据条数
  public curPage:any = 1;// 当前页码
  public totalNum: any = 0; // 总数据条数
  public totalPage: any = 0;// 总页数

  public url:string = this.httpURLService.getUrl("communication");

  constructor(private http: HttpClient,private httpURLService: HttpURLService){

  }
  add(){
    this.addMessages =  new SwitchFactor("","","","","","");
  }
  userMessage(event:SwitchFactor){
    let query = new SwitchFactor("","","","","","");
    let update = new SwitchFactor("","","","","","");
    Object.assign(query, event);
    Object.assign(update, event);
    this.queryMessage = query;
    this.updateMessages = update;
  }

  listMessage(info:any){
    let switchFactorCode = info.switchFactorCode== undefined ? '' : info.switchFactorCode ;
    let switchFactorName = info.switchFactorName== undefined ? '' : info.switchFactorName;
    let current = this.curPage;
    this.http.get(this.url+'/switchFactor/querySwitchFactorListByPage?switchFactorCode='+switchFactorCode+'&switchFactorName='+switchFactorName).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

  addMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/switchFactor/addSwitchFactor',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#add").modal('show');
          this.listMessage("");
          $('#addModal').modal('hide');
        }else if(data['resultCode']=='101119401'){
          this.errorMessage = "开关阀门因子编码为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119402'){
          this.errorMessage = "开关阀门因子名称为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119403'){
          this.errorMessage = "开关阀门因子编码已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119404'){
          this.errorMessage = "开关阀门因子名称已存在！";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }

  deleteById(event:any){
    let id = event.target.name;
    this.http.get(this.url+'/switchFactor/deleteSwitchFactor/'+id).subscribe(data =>{
      if(data['resultCode']=='0'){
        $("#delete").modal('show');
        this.listMessage("");
        $('#deleteModal').modal('hide');
      }else if(data['resultCode']=='101119405'){
        this.errorMessage = "该开关阀门因子不存在！";
        $("#error").modal('show');
      }
    })
  }

  updateMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/switchFactor/updateSwitchFactor',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#update").modal('show');
          this.listMessage("");
          $('#updateModal').modal('hide');
        }else if(data['resultCode']=='101119401'){
          this.errorMessage = "开关阀门因子编码为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119402'){
          this.errorMessage = "开关阀门因子名称为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119403'){
          this.errorMessage = "开关阀门因子编码已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119404'){
          this.errorMessage = "开关阀门因子名称已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119405'){
          this.errorMessage = "该开关阀门因子不存在！";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }
  getPageData(pageNo) {
    let vm = this;
    vm.curPage = pageNo;
    let switchFactorCode = this.switchFactorCode== undefined ? '' : this.switchFactorCode ;
    let switchFactorName = this.switchFactorName== undefined ? '' : this.switchFactorName;
    let current = pageNo;
    this.http.get(this.url+'/switchFactor/querySwitchFactorListByPage?switchFactorCode='+switchFactorCode+'&switchFactorName='+switchFactorName+'&current='+current).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

	onstructor(){ }
	ngOnInit(){
    this.http.get(this.url+'/switchFactor/querySwitchFactorListByPage').subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
	}
}

